package com.gexiaobao.pigeon.app.util

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.util.DisplayMetrics
import android.widget.Toast
import me.hgj.jetpackmvvm.ext.util.windowManager
import me.hgj.jetpackmvvm.util.LogUtils
import java.util.UUID


/**
 * created by :
 * @date : 20220915
 * Describe :
 */
object DeviceUtil {
    /**
     * 获取app的名称
     * @param context
     * @return
     */
    fun getAppName(context: Context): String {
        var appName = ""
        try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(
                context.packageName, 0
            )
            val labelRes = packageInfo.applicationInfo.labelRes
            appName = context.resources.getString(labelRes)
        } catch (e: Throwable) {
            e.printStackTrace();
        }
        return appName;
    }

    //获取软件版本号，对应AndroidManifest.xml下android:versionCode
    fun getVersionCode(activity: Activity): Int {
        var versionCodes = 0
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCodes =
                activity.packageManager.getPackageInfo(activity.packageName, 0).versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return versionCodes
    }

    /**
     * 获取版本号名称
     *
     * @param
     * @return
     */
    fun getVerName(activity: Activity): String {
        var verName = ""
        try {
            verName =
                activity.packageManager.getPackageInfo(activity.packageName, 0).versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return verName
    }

    /**
     * Return the version name of device's system.
     *
     * @return the version name of device's system
     */
    val sDKVersionName: String
        get() = Build.VERSION.RELEASE

    /**
     * Return version code of device's system.
     *
     * @return version code of device's system
     */
    val sDKVersionCode: Int
        get() = Build.VERSION.SDK_INT

    /**
     * Return the android id of device.
     *
     * @return the android id of device
     */
    fun getAndroidId(context: Context): String {
        val id = Settings.Secure.getString(
            context.contentResolver,
            Settings.Secure.ANDROID_ID
        )
        return if ("9774d56d682e549c" == id) "" else id ?: ""
    }

    /**
     * Return the manufacturer of the product/hardware.
     *
     * e.g. Xiaomi
     *
     * @return the manufacturer of the product/hardware
     */
    val manufacturer: String
        get() = Build.MANUFACTURER

    /**
     * Return the model of device.
     *
     * e.g. MI2SC
     *
     * @return the model of device
     */
    val model: String
        get() {
            var model = Build.MODEL
            model = model?.trim { it <= ' ' }?.replace("\\s*".toRegex(), "") ?: ""
            return model
        }

    /**
     * Return an ordered list of ABIs supported by this device. The most preferred ABI is the first
     * element in the list.
     *
     * @return an ordered list of ABIs supported by this device
     */
    val aBIs: Array<String>
        get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Build.SUPPORTED_ABIS
        } else {
            if (!TextUtils.isEmpty(Build.CPU_ABI2)) {
                arrayOf(Build.CPU_ABI, Build.CPU_ABI2)
            } else arrayOf(Build.CPU_ABI)
        }

    private fun getUdid(prefix: String, id: String): String {
        return if (id == "") {
            prefix + UUID.randomUUID().toString().replace("-", "")
        } else prefix + UUID.nameUUIDFromBytes(id.toByteArray()).toString()
            .replace("-", "")
    }

    fun initScreen(context: Context) {
        //获取屏幕分辨率
        val metric = DisplayMetrics()
        context.windowManager?.defaultDisplay?.getMetrics(metric)
        val width: Int = metric.widthPixels // 宽度（PX）
        val height: Int = metric.heightPixels // 高度（PX）
        val density: Float = metric.density // 密度（0.75 / 1.0 / 1.5）
        val densityDpi: Int = metric.densityDpi // 密度DPI（120 / 160 / 240）
        //屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        val screenWidth = (width / density).toInt() //屏幕宽度(dp)
        val screenHeight = (height / density).toInt() //屏幕高度(dp)
        Toast.makeText(
            context, """宽度:$width 高度:$height 密度:$density 密度DPI:$densityDpi
                                                        屏幕dp宽度：$screenWidth 屏幕dp高度：$screenHeight""", Toast.LENGTH_LONG
        ).show()
        LogUtils.debugInfo(
            "=======" + """宽度:$width 高度:$height 密度:$density 密度DPI:$densityDpi
                                                        屏幕dp宽度：$screenWidth 屏幕dp高度：$screenHeight"""
        )
    }
}